import { createRouter, createWebHistory } from "vue-router";
import { useStore } from "vuex";
const router = createRouter({
    history: createWebHistory(import.meta.env.BASE_URL),
    routes: [
        {
            path: "/",
            name: "layout",
            component: () => import("@/pages/Layout.vue"),
            redirect: "/index",
            children: [
                {
                    path: "/index",
                    name: "index",
                    component: () => import("@/pages/Index.vue")
                },
                {
                    path: "/userContr",
                    name: "userControl",
                    component: () => import("@/pages/UserContr.vue")
                },
                {
                    path: "/goodsContr",
                    name: "goodsContr",
                    component: () => import("@/pages/GoodsContr.vue")
                },
                {
                    path: "/bannerContr",
                    name: "bannerContr",
                    component: () => import("@/pages/BannerContr.vue")
                },
            ]
        },
        {
            path: "/login",
            name: "login",
            component: () => import("@/pages/Login.vue")
        },
        {
            path: "/:pathMatch(.*)",
            name: "NotFound",
            component: import("@/pages/404.vue")
        },


    ]
})




// 路由守卫
router.beforeEach((to, from, next) => {
    // console.log(this.$store.state);
    // console.log("store", store.state.uInfo)
    // 判断是否登录
    const store = useStore()
    const userInfo = store.state.userInfo;
    if (!userInfo.username) {
        if (to.path === '/login') {
            next();
            return;
        }
        next('/login');
    }
    else {
        next();
    }
})



export default router